import React from 'react';
import { Form, Upload, Button } from 'dw-mx';
import { UploadOutlined } from 'dw-mx-icons';
import { request } from 'dw-mx-request';

export default function upload(props) {
    const reqTarget = props.target;
    const [form] = Form.useForm();
    const doUpload = async () => {
        const values = form.getFieldsValue();
        const formdata = new FormData();
        const name = values['tablefile'][0].name;
        if (name.indexOf('.xls') !== -1) {
            if (name.indexOf('.xlsx') === -1) {
                formdata.append('tablefile', values['tablefile'][0].originFileObj, values['tablefile'][0].name);
                formdata.append('xm', 'abc');
                const ret = await request(`/${reqTarget}/mx/test/tableupload`, formdata);
                props.closeDialog(ret);
            } else {
                alert('请上传格式为.xls的表格文档！');
            }
        } else {
            alert('请上传表格格式的文档！');
        }

    }
    return (
        <>
            <Form form={form}>
                <Form.Item
                    name="tablefile"
                    label="上传表格文件"
                    valuePropName="fileList"
                    getValueFromEvent={(e) => {
                        if (Array.isArray(e)) {
                            return e;
                        }
                        return e && e.fileList;
                    }}
                >
                    <Upload beforeUpload={(file) => false}>
                        <Button>
                            <UploadOutlined /> 选择上传的文件
                        </Button>
                    </Upload>
                </Form.Item>
            </Form>
            <Button onClick={doUpload}>上传</Button>
        </>
    )
}